package com.zx;

import com.zx.dao.SysUserMapper;
import com.zx.entity.UserEntity;
import com.zx.io.Resources;
import com.zx.session.SqlSession;
import com.zx.session.SqlSessionFactory;
import com.zx.session.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * @ProjectName: myjdbc
 * @Package: com.zx
 * @ClassName: Test
 * @Author: 1
 * @Description: 测试用例
 * @Date: 2021/9/6 19:24
 */
public class JDBCTest {

    private SysUserMapper mapper;

    @Before
    public void init() throws PropertyVetoException, DocumentException {
        InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        mapper = sqlSession.getMapper(SysUserMapper.class);
    }

    @Test
    public void findAll() throws Exception {
        List<UserEntity> all = mapper.findAll();
        for (UserEntity user : all
        ) {
            System.out.println(user);
        }
    }


    @Test
    public void insertEntity() throws Exception {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("小小");
        userEntity.setAge(21);
        System.out.println(mapper.insertEntity(userEntity));
    }

    @Test
    public void updateById() throws Exception {
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("小小");
        userEntity.setAge(24);
        userEntity.setId(3);
        System.out.println(mapper.updateById(userEntity));
    }



    @Test
    public void deleteById() throws Exception {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(3);
        System.out.println(mapper.deleteById(userEntity));
    }


}
